package drongo.desktop.utils
{
	
	/**
	 * 字段串工具 
	 * @author Gaara
	 */	
	public class UtilsStr
	{
		/**
		 * 返回文件名 
		 * @param name
		 * @return 
		 */
		public static function getFileName(name:String) : String
		{
			var index:int = name.lastIndexOf("/");
			if(index != -1)
				name = name.substr(index + 1);
			
			index = name.lastIndexOf("\\");
			if(index != -1)
				name = name.substr(index + 1);
			
			index = name.lastIndexOf(".");
			if(index != -1)
				return name.substring(0,index);
			
			return name;
		}
		
		/**
		 * 整理字符串 
		 * @param str
		 * @return 
		 */
		public static function trim(str:String):String
		{
			return trimLeft(trimRight(str));
		}
		
		public static function trimLeft(str:String):String
		{
			var index:int = 0;
			while(index < str.length)
			{
				var char:String = str.charAt(index);
				if(char != " " && char != "\n" && char != "\r")
					break;
				
				index++;
			}
			
			return str.substr(index);
		}
		
		public static function trimRight(str:String):String
		{
			var index:int = str.length - 1;
			while(index >= 0)
			{
				var char:String = str.charAt(index);
				if(char != " " && char != "\n" && char != "\r")
					break;
				
				index--;
			}
			
			return str.substring(0,index+1);
		}
		
		/**
		 * 验证名字 
		 * @param name
		 * @return 
		 */
		public static function validateName(name:String):String
		{
			name = trim(name);
			if(name.length == 0)
				throw new Error("名称不能为空");
			
			if(name.search(/[\:\/\\\*\?<>\|]/) != -1)
				throw new Error("名称包含非法字符");
			
			return name;
		}
	}
}